const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    isFinded:false,
    arrayTestType: ['英语一', '英语二'],
    arrayTestYear: [ '2025', '2024', '2023', '2022', '2021', '2020', '2019', '2018', '2017', '2016', '2015', '2014', '2013', '2012', '2011'],
    index: 1,
    indexyear:0,
    inputWord:'',
    word_id:'',
    word:'',
    audio_text:'',
    audio_fn_us:'',
    audio_fn_uk:'',
    word_explain:'',
    hasFlg:'',
    imgSrc:'/images/play.png'
  },
  pickerTestTypeChange: function(e) {
    this.setData({
      index: e.detail.value,
      /*** 画面清空项目 BEGIN ***/ 
      inputWord:'',
      word_id:'',
      word:'',
      audio_text:'',
      audio_fn_us:'',
      audio_fn_uk:'',
      word_explain:'',
      hasFlg:'',
      isFinded:false,
       /*** 画面清空项目 END ***/ 
    })
  },
  pickerTestYearChange: function(e) {
    let indexyear = e.detail.value;
    this.setData({
      indexyear: indexyear,
      /*** 画面清空项目 BEGIN ***/ 
      inputWord:'',
      word_id:'',
      word:'',
      audio_text:'',
      audio_fn_us:'',
      audio_fn_uk:'',
      word_explain:'',
      hasFlg:'',
      isFinded:false,
      /*** 画面清空项目 END ***/ 
    })
  },
  bindWordInput: function(e){
    this.setData({
      inputWord: e.detail.value
    })
  },
  findWord(){
    let inputWord = this.data.inputWord;
    if(inputWord===''){
      wx.showToast({
        icon:'error',
        title: '请输入单词！',
      })
      return
    }
    let findwordUrl = app.globalData.baseUrl + 'api/findword/'
    const that = this; // 将 this 赋值给 that
    // 查询
    wx.request({
      url: findwordUrl, // 后台API
      data: {
        inputWord:inputWord,
        test_type:this.data.index,
        test_year:this.data.arrayTestYear[this.data.indexyear],
        openid:wx.getStorageSync('openid')
      },
      method:'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      /* 登录成功 */
      success (res){
        // 结果查询成功
        if(res.statusCode == 200){
          that.setData({
            isFinded:true,
            word_id : res.data.id,
            word:res.data.word,
            audio_text:res.data.audio_text,
            audio_fn_us:res.data.audio_fn_us,
            audio_fn_uk:res.data.audio_fn_uk,
            word_explain:res.data.word_explain,
            hasFlg:res.data.hasFlg,
            inputWord:'',          
          }) 
        }else{
          that.setData({
            isFinded:false,
            inputWord:'', 
            hasFlg:'',          
          })          
        }
             
      },
      /* 登录失败 */
      fail:(err)=>{
        that.setData({
          isFinded:false,
          inputWord:'', 
          hasFlg:'',          
        })
      }
    })
  },
addword(){
  let inputWord = this.data.inputWord;
  let findwordUrl = app.globalData.baseUrl + 'api/addword/'
  const that = this; // 将 this 赋值给 that
  wx.request({
    url: findwordUrl, // 后台API
    data: {
      id:this.data.word_id,
      inputWord:inputWord,
      test_type:this.data.index,
      test_year:this.data.arrayTestYear[this.data.indexyear],
      openid:wx.getStorageSync('openid')
    },
    method:'POST',
    header: {
      'content-type': 'application/json' // 默认值
    },
    /* 登录成功 */
    success (res){
      // 结果查询成功
      if(res.statusCode === 200){
        that.setData({
          isFinded:false,
          inputWord:'', 
          hasFlg:'', 
          word:'',
          word_explain:'',         
        }) 
      }else{
        that.setData({
          isFinded:false,
          inputWord:'', 
          hasFlg:'0',          
        })          
      }
          
    },
    /* 登录失败 */
    fail:(err)=>{
      that.setData({
        isFinded:false,
        inputWord:'', 
        hasFlg:'0',          
      })
    }
  })    
  },
delword(){
  let inputWord = this.data.inputWord;
  let findwordUrl = app.globalData.baseUrl + 'api/delword/'
  const that = this; // 将 this 赋值给 that
  wx.request({
    url: findwordUrl, // 后台API
    data: {
      id:this.data.word_id,
      inputWord:inputWord,
      test_type:this.data.index,
      test_year:this.data.arrayTestYear[this.data.indexyear],
      openid:wx.getStorageSync('openid')
    },
    method:'POST',
    header: {
      'content-type': 'application/json' // 默认值
    },
    /* 登录成功 */
    success (res){
      // 结果查询成功
      if(res.statusCode == 200){
        that.setData({
          isFinded:false,
          inputWord:'', 
          hasFlg:'',  
          /*** 画面清空项目 BEGIN ***/ 
          word_id:'',
          word:'',
          audio_text:'',
          audio_fn_us:'',
          audio_fn_uk:'',
          word_explain:'',
          /*** 画面清空项目 END ***/         
        }) 
      }else{
        that.setData({
          isFinded:false,
          inputWord:'', 
          hasFlg:'', 
          /*** 画面清空项目 BEGIN ***/ 
          word_id:'',
          word:'',
          audio_text:'',
          audio_fn_us:'',
          audio_fn_uk:'',
          word_explain:'',
          /*** 画面清空项目 END ***/           
        })          
      }
          
    },
    /* 登录失败 */
    fail:(err)=>{
      that.setData({
        isFinded:false,
        inputWord:'', 
        hasFlg:'',
        /*** 画面清空项目 BEGIN ***/ 
        word_id:'',
        word:'',
        audio_text:'',
        audio_fn_us:'',
        audio_fn_uk:'',
        word_explain:'',
        /*** 画面清空项目 END ***/            
      })
    }
  })    
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let isLoggedIn = getApp().globalData.isLoggedIn
    if(isLoggedIn){
      this.setData({
        isLogin: true
      });
    }else{
      this.setData({
        isLogin: false
      });  
    }
  },
  topCatUs(){
    let audio = wx.createInnerAudioContext()
    audio.src= app.globalData.baseUrlStatic+'mp3file/US/'+ this.data.audio_fn_us
    console.log(audio.src)
    audio.play()
  },
  topCatUk(){
    let audio = wx.createInnerAudioContext()
    audio.src= app.globalData.baseUrlStatic+'mp3file/UK/'+ this.data.audio_fn_uk
    console.log(audio.src)
    audio.play()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})